/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sokoban.stages.controller;

import br.com.sokoban.elements.interfaces.MapCharacters;
import br.com.sokoban.elements.model.Area;
import br.com.sokoban.elements.model.AreaFull;
import br.com.sokoban.elements.model.Free;
import br.com.sokoban.elements.model.Jewels;
import br.com.sokoban.elements.model.Player;
import br.com.sokoban.elements.model.Wall;
import br.com.sokoban.stages.model.Maps;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;

/**
 *
 * @author kevinstortz
 */
public class Board {

    private final int OFFSET = 30;
    private final int SPACE = 20;
    private int w = 0;
    private int h = 0;
    private ArrayList<MapCharacters> world;    
    
    public Board(int mapa) {
        world = new ArrayList<>();
        initWorld(mapa);
        geraMapas(world);
    }
    
    public ArrayList<MapCharacters> getWorld(){
        return world;
    }
    
    private void initWorld(int mapa){
        int x = OFFSET;
        int y = OFFSET;
        
        String[] level = carregarMapa(mapa);

        for (int i = 0; i < level.length; i++) {
            for (int j = 0; j < level[i].length(); j++) {

                char item = level[i].charAt(j);
                
                switch(item){
                    case '#' : world.add(new Wall(i, j));x += SPACE;break;
                    case '0' : world.add(new Free(i, j));x += SPACE;break;  
                    case '@' : world.add(new Player(i, j));x += SPACE;break;
                    case '$' : world.add(new Jewels(i, j));x += SPACE;break;
                    case '%' : world.add(new Area(i, j));x += SPACE;break;
                    case '*' : world.add(new AreaFull(i, j));x += SPACE;break;
                }
            }
        }
        h = level.length;
        w = level[0].length();
    }

    public int getBoardWidth() {
        return this.w;
    }

    public int getBoardHeight() {
        return this.h;
    }

    private String[] carregarMapa(int level) {
        String[] mapa;
        Maps mapas = new Maps();
        mapa = mapas.carregarMapas(level);
        return mapa;
    }

    public Graphics geraMapas(ArrayList<MapCharacters> world) {
        Graphics g = new Graphics();    
        g.setColor(new Color(250, 240, 170));
        g.fillRect(0, 0, w, h);

        for (int i = 0; i < world.size(); i++) {
            MapCharacters item = (MapCharacters) world.get(i);

            if (item instanceof Player) {
                g.drawImage(item.getImage(), item.getX() + 2, item.getY() + 2, null);
            } else {
                if (item instanceof Area) {
                    g.drawImage(item.getImage(), item.getX() + 2, item.getY() + 2, null);
                } else {
                    if (item instanceof Free) {
                        g.drawImage(item.getImage(), item.getX() + 2, item.getY() + 2, null);
                    } else {
                        if (item instanceof AreaFull) {
                            g.drawImage(item.getImage(), item.getX() + 2, item.getY() + 2, null);
                        } else {
                            if (item instanceof Wall) {
                                g.drawImage(item.getImage(), item.getX() + 2, item.getY() + 2, null);
                            }
                        }
                    }
                }
            }
        }
        return g;
        
    }
}
